home *** CD-ROM | disk | FTP | other *** search
/ Super PC 33 / Super PC 33 (Shareware).iso / spc / sonido / timidity / source / playmidi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-03  |  21.1 KB  |  905 lines

  1. /*
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <titoivon@snakemail.hut.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     playmidi.c -- random stuff in need of rearrangement
  21.  
  22. */
  23.  
  24. #include <stdio.h>
  25. #ifdef __WIN32__
  26. #include <string.h>
  27. #else
  28. #include <unistd.h>
  29. #include <strings.h>
  30. #endif
  31. #include "config.h"
  32. #include "common.h"
  33. #ifdef __WIN32__
  34. #include "instrume.h"
  35. #else
  36. #include "instruments.h"
  37. #endif
  38. #include "playmidi.h"
  39. #include "readmidi.h"
  40. #include "output.h"
  41. #include "mix.h"
  42. #include "controls.h"
  43.  
  44. #include "tables.h"
  45.  
  46. #ifdef __WIN32__
  47. extern int intr;
  48. #endif
  49.  
  50. Channel channel[16];
  51. Voice voice[MAX_VOICES];
  52.  
  53. int
  54.     voices=DEFAULT_VOICES;
  55.  
  56. int32
  57.     control_ratio=0,
  58.     amplification=DEFAULT_AMPLIFICATION;
  59.  
  60. float
  61.     master_volume;
  62.  
  63. int32 drumchannels=DEFAULT_DRUMCHANNELS;
  64. int adjust_panning_immediately=1;
  65.  
  66. static int32 lost_notes, cut_notes;
  67. static int32 common_buffer[AUDIO_BUFFER_SIZE*2], /* stereo samples */
  68.              *buffer_pointer;
  69. static int32 buffered_count;
  70.  
  71. static MidiEvent *event_list, *current_event;
  72. static int32 sample_count, current_sample;
  73.  
  74. static void adjust_amplification(void)
  75.   master_volume = (double)(amplification) / 100.0L;
  76. }
  77.  
  78. static void reset_voices(void)
  79. {
  80.   int i;
  81.   for (i=0; i<MAX_VOICES; i++)
  82.     voice[i].status=VOICE_FREE;
  83. }
  84.  
  85. /* Process the Reset All Controllers event */
  86. static void reset_controllers(int c)
  87. {
  88.   channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */
  89.   channel[c].expression=127; /* SCC-1 does this. */
  90.   channel[c].sustain=0;
  91.   channel[c].pitchbend=0x2000;
  92.   channel[c].pitchfactor=0; /* to be computed */
  93. }
  94.  
  95. static void redraw_controllers(int c)
  96. {
  97.   ctl->volume(c, channel[c].volume);
  98.   ctl->expression(c, channel[c].expression);
  99.   ctl->sustain(c, channel[c].sustain);
  100.   ctl->pitch_bend(c, channel[c].pitchbend);
  101. }
  102.  
  103. static void reset_midi(void)
  104. {
  105.   int i;
  106.   for (i=0; i<16; i++)
  107.     {
  108.       reset_controllers(i);
  109.       /* The rest of these are unaffected by the Reset All Controllers event */
  110.       channel[i].program=default_program;
  111.       channel[i].panning=NO_PANNING;
  112.       channel[i].pitchsens=2;
  113.       channel[i].bank=0; /* tone bank or drum set */
  114.     }
  115.   reset_voices();
  116. }
  117.  
  118. static void select_sample(int v, Instrument *ip)
  119. {
  120.   int32 f, cdiff, diff;
  121.   int s,i;
  122.   Sample *sp, *closest;
  123.  
  124.   s=ip->samples;
  125.   sp=ip->sample;
  126.  
  127.   if (s==1)
  128.     {
  129.       voice[v].sample=sp;
  130.       return;
  131.     }
  132.  
  133.   f=voice[v].orig_frequency;
  134.   for (i=0; i<s; i++)
  135.     {
  136.       if (sp->low_freq <= f && sp->high_freq >= f)
  137.     {
  138.       voice[v].sample=sp;
  139.       return;
  140.     }
  141.       sp++;
  142.     }
  143.  
  144.   /* 
  145.      No suitable sample found! We'll select the sample whose root
  146.      frequency is closest to the one we want. (Actually we should
  147.      probably convert the low, high, and root frequencies to MIDI note
  148.      values and compare those.) */
  149.  
  150.   cdiff=0x7FFFFFFF;
  151.   closest=sp=ip->sample;
  152.   for(i=0; i<s; i++)
  153.     {
  154.       diff=sp->root_freq - f;
  155.       if (diff<0) diff=-diff;
  156.       if (diff<cdiff)
  157.     {
  158.       cdiff=diff;
  159.       closest=sp;
  160.     }
  161.       sp++;
  162.     }
  163.   voice[v].sample=closest;
  164.   return;
  165. }
  166.  
  167. static void recompute_freq(int v)
  168. {
  169.   int 
  170.     sign=(voice[v].sample_increment < 0), /* for bidirectional loops */
  171.     pb=channel[voice[v].channel].pitchbend;
  172.   double a;
  173.   
  174.   if (!voice[v].sample->sample_rate)
  175.     return;
  176.  
  177.   if (voice[v].vibrato_control_ratio)
  178.     {
  179.       /* This instrument has vibrato. Invalidate any precomputed
  180.          sample_increments. */
  181.  
  182.       int i=VIBRATO_SAMPLE_INCREMENTS;
  183.       while (i--)
  184.     voice[v].vibrato_sample_increment[i]=0;
  185.     }
  186.  
  187.   if (pb==0x2000 || pb<0 || pb>0x3FFF)
  188.     voice[v].frequency=voice[v].orig_frequency;
  189.   else
  190.     {
  191.       pb-=0x2000;
  192.       if (!(channel[voice[v].channel].pitchfactor))
  193.     {
  194.       /* Damn. Somebody bent the pitch. */
  195.       int32 i=pb*channel[voice[v].channel].pitchsens;
  196.       if (pb<0)
  197.         i=-i;
  198.       channel[voice[v].channel].pitchfactor=
  199.         bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13];
  200.     }
  201.       if (pb>0)
  202.     voice[v].frequency=
  203.       (int32)(channel[voice[v].channel].pitchfactor *
  204.           (double)(voice[v].orig_frequency));
  205.       else
  206.     voice[v].frequency=
  207.       (int32)((double)(voice[v].orig_frequency) /
  208.           channel[voice[v].channel].pitchfactor);
  209.     }
  210.  
  211.   a = FSCALE(((double)(voice[v].sample->sample_rate) *
  212.           (double)(voice[v].frequency)) /
  213.          ((double)(voice[v].sample->root_freq) *
  214.           (double)(play_mode->rate)),
  215.          FRACTION_BITS);
  216.  
  217.   if (sign) 
  218.     a = -a; /* need to preserve the loop direction */
  219.  
  220.   voice[v].sample_increment = (int32)(a);
  221. }
  222.  
  223. static void recompute_amp(int v)
  224. {
  225.   int32 tempamp;
  226.  
  227.   /* TODO: use fscale */
  228.  
  229.   tempamp= (voice[v].velocity *
  230.         channel[voice[v].channel].volume * 
  231.         channel[voice[v].channel].expression); /* 21 bits */
  232.  
  233.   if (!(play_mode->encoding & PE_MONO))
  234.     {
  235.       if (voice[v].panning > 60 && voice[v].panning < 68)
  236.     {
  237.       voice[v].panned=PANNED_CENTER;
  238.  
  239.       voice[v].left_amp=
  240.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  241.               21);
  242.     }
  243.       else if (voice[v].panning<5)
  244.     {
  245.       voice[v].panned = PANNED_LEFT;
  246.  
  247.       voice[v].left_amp=
  248.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  249.               20);
  250.     }
  251.       else if (voice[v].panning>123)
  252.     {
  253.       voice[v].panned = PANNED_RIGHT;
  254.  
  255.       voice[v].left_amp= /* left_amp will be used */
  256.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  257.               20);
  258.     }
  259.       else
  260.     {
  261.       voice[v].panned = PANNED_MYSTERY;
  262.  
  263.       voice[v].left_amp=
  264.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  265.               27);
  266.       voice[v].right_amp=voice[v].left_amp * (voice[v].panning);
  267.       voice[v].left_amp *= (double)(127-voice[v].panning);
  268.     }
  269.     }
  270.   else
  271.     {
  272.       voice[v].panned=PANNED_CENTER;
  273.  
  274.       voice[v].left_amp=
  275.     FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  276.           21);
  277.     }
  278. }
  279.  
  280. static void start_note(MidiEvent *e, int i)
  281. {
  282.   Instrument *ip;
  283.   int j;
  284.  
  285.   if (ISDRUMCHANNEL(e->channel))
  286.     {
  287.       if (!(ip=drumset[channel[e->channel].bank]->tone[e->a].instrument))
  288.     {
  289.       if (!(ip=drumset[0]->tone[e->a].instrument))
  290.         return; /* No instrument? Then we can't play. */
  291.     }
  292.       if (ip->samples != 1)
  293.     {
  294.       ctl->cmsg(CMSG_WARNING, VERB_VERBOSE, 
  295.            "Strange: percussion instrument with %d samples!", ip->samples);
  296.     }
  297.  
  298.       if (ip->sample->note_to_use) /* Do we have a fixed pitch? */
  299.     voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
  300.       else
  301.     voice[i].orig_frequency=freq_table[e->a & 0x7F];
  302.       
  303.       /* drums are supposed to have only one sample */
  304.       voice[i].sample=ip->sample;
  305.     }
  306.   else
  307.     {
  308.       if (channel[e->channel].program==SPECIAL_PROGRAM)
  309.     ip=default_instrument;
  310.       else if (!(ip=tonebank[channel[e->channel].bank]->
  311.          tone[channel[e->channel].program].instrument))
  312.     {
  313.       if (!(ip=tonebank[0]->tone[channel[e->channel].program].instrument))
  314.         return; /* No instrument? Then we can't play. */
  315.     }
  316.  
  317.       if (ip->sample->note_to_use) /* Fixed-pitch instrument? */
  318.     voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
  319.       else
  320.     voice[i].orig_frequency=freq_table[e->a & 0x7F];
  321.       select_sample(i, ip);
  322.     }
  323.  
  324.   voice[i].status=VOICE_ON;
  325.   voice[i].channel=e->channel;
  326.   voice[i].note=e->a;
  327.   voice[i].velocity=e->b;
  328.   voice[i].sample_offset=0;
  329.   voice[i].sample_increment=0; /* make sure it isn't negative */
  330.  
  331.   voice[i].tremolo_phase=0;
  332.   voice[i].tremolo_phase_increment=voice[i].sample->tremolo_phase_increment;
  333.   voice[i].tremolo_sweep=voice[i].sample->tremolo_sweep_increment;
  334.   voice[i].tremolo_sweep_position=0;
  335.  
  336.   voice[i].vibrato_sweep=voice[i].sample->vibrato_sweep_increment;
  337.   voice[i].vibrato_sweep_position=0;
  338.   voice[i].vibrato_control_ratio=voice[i].sample->vibrato_control_ratio;
  339.   voice[i].vibrato_control_counter=voice[i].vibrato_phase=0;
  340.   for (j=0; j<VIBRATO_SAMPLE_INCREMENTS; j++)
  341.     voice[i].vibrato_sample_increment[j]=0;
  342.  
  343.   if (channel[e->channel].panning != NO_PANNING)
  344.     voice[i].panning=channel[e->channel].panning;
  345.   else
  346.     voice[i].panning=voice[i].sample->panning;
  347.  
  348.   recompute_freq(i);
  349.   recompute_amp(i);
  350.   if (voice[i].sample->modes & MODES_ENVELOPE)
  351.     {
  352.       /* Ramp up from 0 */
  353.       voice[i].envelope_stage=0;
  354.       voice[i].envelope_volume=0;
  355.       voice[i].control_counter=0;
  356.       recompute_envelope(i);
  357.       apply_envelope_to_amp(i);
  358.     }
  359.   else
  360.     {
  361.       voice[i].envelope_increment=0;
  362.       apply_envelope_to_amp(i);
  363.     }
  364.   ctl->note(i);
  365. }
  366.  
  367. static void kill_note(int i)
  368. {
  369.   voice[i].status=VOICE_DIE;
  370.   ctl->note(i);
  371. }
  372.  
  373. /* Only one instance of a note can be playing on a single channel. */
  374. static void note_on(MidiEvent *e)
  375. {
  376.   int i=voices, lowest=-1; 
  377.   int32 lv=0x7FFFFFFF, v;
  378.  
  379.   while (i--)
  380.     {
  381.       if (voice[i].status == VOICE_FREE)
  382.     lowest=i; /* Can't get a lower volume than silence */
  383.       else if (voice[i].channel==e->channel && 
  384.            (voice[i].note==e->a || channel[voice[i].channel].mono))
  385.     kill_note(i);
  386.     }
  387.  
  388.   if (lowest != -1)
  389.     {
  390.       /* Found a free voice. */
  391.       start_note(e,lowest);
  392.       return;
  393.     }
  394.   
  395.   /* Look for the decaying note with the lowest volume */
  396.   i=voices;
  397.   while (i--)
  398.     {
  399.       if ((voice[i].status!=VOICE_ON) &&
  400.       (voice[i].status!=VOICE_DIE))
  401.     {
  402.       v=voice[i].left_mix;
  403.       if ((voice[i].panned==PANNED_MYSTERY) && (voice[i].right_mix>v))
  404.         v=voice[i].right_mix;
  405.       if (v<lv)
  406.         {
  407.           lv=v;
  408.           lowest=i;
  409.         }
  410.     }
  411.     }
  412.  
  413.   if (lowest != -1)
  414.     {
  415.       /* This can still cause a click, but if we had a free voice to
  416.      spare for ramping down this note, we wouldn't need to kill it
  417.      in the first place... Still, this needs to be fixed. Perhaps
  418.      we could use a reserve of voices to play dying notes only. */
  419.       
  420.       cut_notes++;
  421.       voice[lowest].status=VOICE_FREE;
  422.       ctl->note(lowest);
  423.       start_note(e,lowest);
  424.     }
  425.   else
  426.     lost_notes++;
  427. }
  428.  
  429. static void finish_note(int i)
  430. {
  431.   if (voice[i].sample->modes & MODES_ENVELOPE)
  432.     {
  433.       /* We need to get the envelope out of Sustain stage */
  434.       voice[i].envelope_stage=3;
  435.       voice[i].status=VOICE_OFF;
  436.       recompute_envelope(i);
  437.       apply_envelope_to_amp(i);
  438.       ctl->note(i);
  439.     }
  440.   else
  441.     {
  442.       /* Set status to OFF so resample_voice() will let this voice out
  443.          of its loop, if any. In any case, this voice dies when it
  444.          hits the end of its data (ofs>=data_length). */
  445.       voice[i].status=VOICE_OFF;
  446.     }
  447. }
  448.  
  449. static void note_off(MidiEvent *e)
  450. {
  451.   int i=voices;
  452.   while (i--)
  453.     if (voice[i].status==VOICE_ON &&
  454.     voice[i].channel==e->channel &&
  455.     voice[i].note==e->a)
  456.       {
  457.     if (channel[e->channel].sustain)
  458.       {
  459.         voice[i].status=VOICE_SUSTAINED;
  460.         ctl->note(i);
  461.       }
  462.     else
  463.       finish_note(i);
  464.     return;
  465.       }
  466. }
  467.  
  468. /* Process the All Notes Off event */
  469. static void all_notes_off(int c)
  470. {
  471.   int i=voices;
  472.   ctl->cmsg(CMSG_INFO, VERB_DEBUG, "All notes off on channel %d", c);
  473.   while (i--)
  474.     if (voice[i].status==VOICE_ON &&
  475.     voice[i].channel==c)
  476.       {
  477.     if (channel[c].sustain) 
  478.       {
  479.         voice[i].status=VOICE_SUSTAINED;
  480.         ctl->note(i);
  481.       }
  482.     else
  483.       finish_note(i);
  484.       }
  485. }
  486.  
  487. /* Process the All Sounds Off event */
  488. static void all_sounds_off(int c)
  489. {
  490.   int i=voices;
  491.   while (i--)
  492.     if (voice[i].channel==c && 
  493.     voice[i].status != VOICE_FREE &&
  494.     voice[i].status != VOICE_DIE)
  495.       {
  496.     kill_note(i);
  497.       }
  498. }
  499.  
  500. static void adjust_pressure(MidiEvent *e)
  501. {
  502.   int i=voices;
  503.   while (i--)
  504.     if (voice[i].status==VOICE_ON &&
  505.     voice[i].channel==e->channel &&
  506.     voice[i].note==e->a)
  507.       {
  508.     voice[i].velocity=e->b;
  509.     recompute_amp(i);
  510.     apply_envelope_to_amp(i);
  511.     return;
  512.       }
  513. }
  514.  
  515. static void adjust_panning(int c)
  516. {
  517.   int i=voices;
  518.   while (i--)
  519.     if ((voice[i].channel==c) &&
  520.     (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
  521.       {
  522.     voice[i].panning=channel[c].panning;
  523.     recompute_amp(i);
  524.     apply_envelope_to_amp(i);
  525.       }
  526. }
  527.  
  528. static void drop_sustain(int c)
  529. {
  530.   int i=voices;
  531.   while (i--)
  532.     if (voice[i].status==VOICE_SUSTAINED && voice[i].channel==c)
  533.       finish_note(i);
  534. }
  535.  
  536. static void adjust_pitchbend(int c)
  537. {
  538.   int i=voices;
  539.   while (i--)
  540.     if (voice[i].status!=VOICE_FREE && voice[i].channel==c)
  541.       {
  542.     recompute_freq(i);
  543.       }
  544. }
  545.  
  546. static void adjust_volume(int c)
  547. {
  548.   int i=voices;
  549.   while (i--)
  550.     if (voice[i].channel==c &&
  551.     (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
  552.       {
  553.     recompute_amp(i);
  554.     apply_envelope_to_amp(i);
  555.       }
  556. }
  557.  
  558. static void seek_forward(int32 until_time)
  559. {
  560.   reset_voices();
  561.   while (current_event->time < until_time)
  562.     {
  563.       switch(current_event->type)
  564.     {
  565.       /* All notes stay off. Just handle the parameter changes. */
  566.  
  567.     case ME_PITCH_SENS:
  568.       channel[current_event->channel].pitchsens=
  569.         current_event->a;
  570.       channel[current_event->channel].pitchfactor=0;
  571.       break;
  572.       
  573.     case ME_PITCHWHEEL:
  574.       channel[current_event->channel].pitchbend=
  575.         current_event->a + current_event->b * 128;
  576.       channel[current_event->channel].pitchfactor=0;
  577.       break;
  578.       
  579.     case ME_MAINVOLUME:
  580.       channel[current_event->channel].volume=current_event->a;
  581.       break;
  582.       
  583.     case ME_PAN:
  584.       channel[current_event->channel].panning=current_event->a;
  585.       break;
  586.           
  587.     case ME_EXPRESSION:
  588.       channel[current_event->channel].expression=current_event->a;
  589.       break;
  590.       
  591.     case ME_PROGRAM:
  592.       if (ISDRUMCHANNEL(current_event->channel))
  593.         /* Change drum set */
  594.         channel[current_event->channel].bank=current_event->a;
  595.       else
  596.         channel[current_event->channel].program=current_event->a;
  597.       break;
  598.  
  599.     case ME_SUSTAIN:
  600.       channel[current_event->channel].sustain=current_event->a;
  601.       break;
  602.  
  603.     case ME_RESET_CONTROLLERS:
  604.       reset_controllers(current_event->channel);
  605.       break;
  606.           
  607.     case ME_TONE_BANK:
  608.       channel[current_event->channel].bank=current_event->a;
  609.       break;
  610.       
  611.     case ME_EOT:
  612.       current_sample=current_event->time;
  613.       return;
  614.     }
  615.       current_event++;
  616.     }
  617.   /*current_sample=current_event->time;*/
  618.   if (current_event != event_list)
  619.     current_event--;
  620.   current_sample=until_time;
  621. }
  622.  
  623. static void skip_to(int32 until_time)
  624. {
  625.   if (current_sample > until_time)
  626.     current_sample=0;
  627.  
  628.   reset_midi();
  629.   buffered_count=0;
  630.   buffer_pointer=common_buffer;
  631.   current_event=event_list;
  632.   
  633.   if (until_time)
  634.     seek_forward(until_time);
  635.   ctl->reset();
  636. }
  637.  
  638. static int apply_controls(void)
  639. {
  640.   int rc, i, did_skip=0;
  641.   int32 val;
  642.   /* ASCII renditions of CD player pictograms indicate approximate effect */
  643.   do
  644.     switch(rc=ctl->read(&val))
  645.       {
  646.       case RC_QUIT: /* [] */
  647.       case RC_NEXT: /* >>| */
  648.       case RC_REALLY_PREVIOUS: /* |<< */
  649.     play_mode->purge_output();
  650.     return rc;
  651.     
  652.       case RC_CHANGE_VOLUME:
  653.     if (val>0 || amplification > -val)
  654.       amplification += val;
  655.     else 
  656.       amplification=0;
  657.     if (amplification > MAX_AMPLIFICATION)
  658.       amplification=MAX_AMPLIFICATION;
  659.     adjust_amplification();
  660.     for (i=0; i<voices; i++)
  661.       if (voice[i].status != VOICE_FREE)
  662.         {
  663.           recompute_amp(i);
  664.           apply_envelope_to_amp(i);
  665.         }
  666.     ctl->master_volume(amplification);
  667.     break;
  668.  
  669.       case RC_PREVIOUS: /* |<< */
  670.     play_mode->purge_output();
  671.     if (current_sample < 2*play_mode->rate)
  672.       return RC_REALLY_PREVIOUS;
  673.     return RC_RESTART;
  674.  
  675.       case RC_RESTART: /* |<< */
  676.     skip_to(0);
  677.     play_mode->purge_output();
  678.     return rc;
  679.     
  680.       case RC_JUMP:
  681.     play_mode->purge_output();
  682.     if (val >= sample_count)
  683.       return RC_NEXT;
  684.     skip_to(val);
  685.     return rc;
  686.     
  687.       case RC_FORWARD: /* >> */
  688.     /*play_mode->purge_output();*/
  689.     if (val+current_sample >= sample_count)
  690.       return RC_NEXT;
  691.     skip_to(val+current_sample);
  692.     did_skip=1;
  693.     break;
  694.     
  695.       case RC_BACK: /* << */
  696.     /*play_mode->purge_output();*/
  697.     if (current_sample > val)
  698.       skip_to(current_sample-val);
  699.     else
  700.       skip_to(0); /* We can't seek to end of previous song. */
  701.     did_skip=1;
  702.     break;
  703.       }
  704.   while (rc!= RC_NONE);
  705.   if (did_skip)
  706.     return RC_JUMP; /* could have been RC_BACK or RC_FORWARD, not important */
  707.   else
  708.     return rc;
  709. }
  710.  
  711. static void do_compute_data(int32 count)
  712. {
  713.   int i;
  714.   memset(buffer_pointer, 0, 
  715.      (play_mode->encoding & PE_MONO) ? (count * 4) : (count * 8));
  716.   for (i=0; i<voices; i++)
  717.     {
  718.       if(voice[i].status != VOICE_FREE)
  719.     mix_voice(buffer_pointer, i, count);
  720.     }
  721.   current_sample += count;
  722. }
  723.  
  724. /* count=0 means flush remaining buffered data to output device, then
  725.    flush the device itself */
  726. static int compute_data(int32 count)
  727. {
  728.   int rc;
  729.   if (!count)
  730.     {
  731.       if (buffered_count)
  732.     play_mode->output_data(common_buffer, buffered_count);
  733.       play_mode->flush_output();
  734.       buffer_pointer=common_buffer;
  735.       buffered_count=0;
  736.       return RC_NONE;
  737.     }
  738.  
  739.   while ((count+buffered_count) >= AUDIO_BUFFER_SIZE)
  740.     {
  741.       do_compute_data(AUDIO_BUFFER_SIZE-buffered_count);
  742.       count -= AUDIO_BUFFER_SIZE-buffered_count;
  743.       play_mode->output_data(common_buffer, AUDIO_BUFFER_SIZE);
  744.       buffer_pointer=common_buffer;
  745.       buffered_count=0;
  746.       
  747.       ctl->current_time(current_sample);
  748.       if ((rc=apply_controls())!=RC_NONE)
  749.     return rc;
  750.     }
  751.   if (count>0)
  752.     {
  753.       do_compute_data(count);
  754.       buffered_count += count;
  755.       buffer_pointer += (play_mode->encoding & PE_MONO) ? count : count*2;
  756.     }
  757.   return RC_NONE;
  758. }
  759.  
  760. int play_midi(MidiEvent *eventlist, int32 events, int32 samples)
  761. {
  762.   int rc;
  763.  
  764.   adjust_amplification();
  765.  
  766.   sample_count=samples;
  767.   event_list=eventlist;
  768.   lost_notes=cut_notes=0;
  769.  
  770.   skip_to(0);
  771.   
  772.   for (;;)
  773.      {
  774. #ifdef __WIN32__
  775.         /* check break signals */ 
  776.         if (intr)
  777.             return RC_QUIT;
  778. #endif
  779.         /* Handle all events that should happen at this time */
  780.         while (current_event->time <= current_sample)
  781.     {
  782.       switch(current_event->type)
  783.         {
  784.  
  785.           /* Effects affecting a single note */
  786.  
  787.         case ME_NOTEON:
  788.           if (!(current_event->b)) /* Velocity 0? */
  789.         note_off(current_event);
  790.           else
  791.         note_on(current_event);
  792.           break;
  793.  
  794.         case ME_NOTEOFF:
  795.           note_off(current_event);
  796.           break;
  797.  
  798.         case ME_KEYPRESSURE:
  799.           adjust_pressure(current_event);
  800.           break;
  801.  
  802.           /* Effects affecting a single channel */
  803.  
  804.         case ME_PITCH_SENS:
  805.           channel[current_event->channel].pitchsens=
  806.         current_event->a;
  807.           channel[current_event->channel].pitchfactor=0;
  808.           break;
  809.           
  810.         case ME_PITCHWHEEL:
  811.           channel[current_event->channel].pitchbend=
  812.         current_event->a + current_event->b * 128;
  813.           channel[current_event->channel].pitchfactor=0;
  814.           /* Adjust pitch for notes already playing */
  815.           adjust_pitchbend(current_event->channel);
  816.           ctl->pitch_bend(current_event->channel, 
  817.                   channel[current_event->channel].pitchbend);
  818.           break;
  819.           
  820.         case ME_MAINVOLUME:
  821.           channel[current_event->channel].volume=current_event->a;
  822.           adjust_volume(current_event->channel);
  823.           ctl->volume(current_event->channel, current_event->a);
  824.           break;
  825.           
  826.         case ME_PAN:
  827.           channel[current_event->channel].panning=current_event->a;
  828.           if (adjust_panning_immediately)
  829.         adjust_panning(current_event->channel);
  830.           ctl->panning(current_event->channel, current_event->a);
  831.           break;
  832.           
  833.         case ME_EXPRESSION:
  834.           channel[current_event->channel].expression=current_event->a;
  835.           adjust_volume(current_event->channel);
  836.           ctl->expression(current_event->channel, current_event->a);
  837.           break;
  838.  
  839.         case ME_PROGRAM:
  840.           if (ISDRUMCHANNEL(current_event->channel))
  841.         {
  842.           /* Change drum set */
  843.           channel[current_event->channel].bank=current_event->a;
  844.         }
  845.           else
  846.         {
  847.           channel[current_event->channel].program=current_event->a;
  848.         }
  849.           ctl->program(current_event->channel, current_event->a);
  850.           break;
  851.  
  852.         case ME_SUSTAIN:
  853.           channel[current_event->channel].sustain=current_event->a;
  854.           if (!current_event->a)
  855.         drop_sustain(current_event->channel);
  856.           ctl->sustain(current_event->channel, current_event->a);
  857.           break;
  858.           
  859.         case ME_RESET_CONTROLLERS:
  860.           reset_controllers(current_event->channel);
  861.           redraw_controllers(current_event->channel);
  862.           break;
  863.  
  864.         case ME_ALL_NOTES_OFF:
  865.           all_notes_off(current_event->channel);
  866.           break;
  867.           
  868.         case ME_ALL_SOUNDS_OFF:
  869.           all_sounds_off(current_event->channel);
  870.           break;
  871.           
  872.         case ME_TONE_BANK:
  873.           channel[current_event->channel].bank=current_event->a;
  874.           break;
  875.  
  876.         case ME_EOT:
  877.           /* Give the last notes a couple of seconds to decay  */
  878.           compute_data(play_mode->rate * 2);
  879.           compute_data(0); /* flush buffer to device */
  880.           ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  881.            "Playing time: ~%d seconds",
  882.            current_sample/play_mode->rate+2);
  883.           ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  884.            "Notes cut: %d", cut_notes);
  885.           ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  886.            "Notes lost totally: %d", lost_notes);
  887.           return RC_NEXT;
  888.         }
  889.       current_event++;
  890.     }
  891.  
  892.       rc=compute_data(current_event->time - current_sample);
  893.       /*current_sample=current_event->time;*/
  894.       ctl->refresh();
  895.       switch(rc)
  896.     {
  897.     case RC_QUIT:
  898.     case RC_NEXT:
  899.     case RC_REALLY_PREVIOUS:
  900.       return rc;
  901.     }
  902.     }
  903. }
  904.